﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Web;

namespace Rose.Models
{
    public class ImageCut
    {
        private ImageFormat GetImageFormat(string path)
        {
            //get extension from path to determine contentType
            string[] parts = path.Split('.');
            string extension = parts[parts.Length - 1];
            ImageFormat imageFormat;

            switch (extension.ToLower())
            {
                case "jpeg":
                case "jpg":
                    imageFormat = ImageFormat.Jpeg;
                    break;

                case "gif":
                    imageFormat = ImageFormat.Gif;
                    break;
                case "png":
                    imageFormat = ImageFormat.Png;
                    break;
                default:
                    throw new NotImplementedException(extension + " not handled");
            }

            return imageFormat;
        }


        public bool CutHttpPostImage(HttpPostedFile file, int x, int y, int width, int height, string fileSaveUrl)
        {
            Image initImage = Image.FromStream(file.InputStream, true);

            if (initImage == null)
                return false;
            if (initImage.Width < x + width || initImage.Height < y + height)
            {

                return false;

            }

            Rectangle fromR = new Rectangle(x, y, width, height);
            Rectangle toR = new Rectangle(0, 0, width, height);

            Image toImage = new System.Drawing.Bitmap(width, height);
            Graphics toG = System.Drawing.Graphics.FromImage(toImage);
            toG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            toG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            toG.Clear(Color.Transparent);
            toG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
            toImage.Save(fileSaveUrl, GetImageFormat(file.FileName));

            toImage.Dispose();
            toG.Dispose();
            initImage.Dispose();
            return true;
        }


        public  bool CutImage(string serverfilename, int x, int y, int width, int height, string fileSaveUrl)
        {
            Image initImage = Image.FromFile(serverfilename, true);

            if (initImage == null)
                return false;
            if (initImage.Width < x + width || initImage.Height < y + height)
            {

                return false;

            }

            Rectangle fromR = new Rectangle(x, y, width, height);
            Rectangle toR = new Rectangle(0, 0, width, height);
         
            Image toImage = new System.Drawing.Bitmap(width, height);
            Graphics toG = System.Drawing.Graphics.FromImage(toImage);
            toG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            toG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            toG.Clear(Color.Transparent);
            toG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
            toImage.Save(fileSaveUrl, GetImageFormat(serverfilename));

            toImage.Dispose();
            toG.Dispose();
            initImage.Dispose();
            return true;

        }
    }
}